﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Security.Cryptography;
using System.IO;

namespace SysDataSource
{
    public static class Utility
    {
        internal static string GetTaskGuid()
        {
            return "583A3358-4F66-44E7-B861-05F1D112E1BD";
        }
        internal static string GetConnStr(string param)
        {
            return ConnStr + param + "\"";
        }
        internal static string ConnStr = "metadata=res://*/ORGEDM.csdl|res://*/ORGEDM.ssdl|res://*/ORGEDM.msl;provider=System.Data.SqlClient;provider connection string=\"";
        
        internal static bool MatchTime(string encrytedData, string timeOut)
        {
            try
            {
                var seconds = Convert.ToInt32(timeOut);
                var dTime = DateTime.Parse(encrytedData);
                var dTimeNow = DateTime.Now;
                TimeSpan ts = dTimeNow - dTime;
                var deltSecs = ts.TotalSeconds;
                return deltSecs <= checked(seconds);
            }
            catch
            {
                return false;
            }
        }
        internal static string EncryptByPublicKey(string xmlPublicKey, string cleanString)
        {
            var byteConverter = new UTF8Encoding();
            var dataToEncrypt = byteConverter.GetBytes(cleanString);

// ReSharper disable InconsistentNaming
            var RSA = new RSACryptoServiceProvider();
// ReSharper restore InconsistentNaming
            RSA.FromXmlString(xmlPublicKey);

            var encryptData = RSA.Encrypt(dataToEncrypt, false);
            var encryptString = Convert.ToBase64String(encryptData);
            return encryptString;
        }
        internal static string Encode(string str, string key)
        {
            var keyStr = GetKeyStr(key);
            var provider = new DESCryptoServiceProvider
            {
                Key = Encoding.ASCII.GetBytes(keyStr),
                IV = Encoding.ASCII.GetBytes(keyStr)
            };
            var bytes = Encoding.GetEncoding("UTF-8").GetBytes(str);
            var stream = new MemoryStream();
            var stream2 = new CryptoStream(stream, provider.CreateEncryptor(), CryptoStreamMode.Write);
            stream2.Write(bytes, 0, bytes.Length);
            stream2.FlushFinalBlock();
            var builder = new StringBuilder();
            foreach (var num in stream.ToArray())
            {
                builder.AppendFormat("{0:X2}", num);
            }
            stream.Close();
            return builder.ToString();
        }
        private static string GetKeyStr(string key)
        {
            var length = key.Length;
            var flag = 8 - length;
            var result = key;
            if (length > 8)
                return key.Substring(0, 8);
            while (flag > 0)
            {
                result += "*";
                flag--;
            }
            return result;
        }
        internal static string CheckValue(object o)
        {
            return Convert.ToString(o);
        }
        internal static string CheckXmlData(System.Xml.XmlNode xmlNode,string key)
        {
            if(null==xmlNode.SelectSingleNode(key))
            {
                var strKey = key.ToLower();
// ReSharper disable PossibleNullReferenceException
                return null == xmlNode.SelectSingleNode(strKey) ? string.Empty : xmlNode.SelectSingleNode(strKey).InnerXml;
// ReSharper restore PossibleNullReferenceException
            }
// ReSharper disable PossibleNullReferenceException
            return xmlNode.SelectSingleNode(key).InnerXml;
// ReSharper restore PossibleNullReferenceException
        }
    }
    public enum TypeEnum 
    {
        TEXT=0,
        LONG,
        DOUBLE,
        DATA,
        BINARY,
        CURRENCY,
        LOGICAL,
        GUID
    }
}
